package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.VariousKindsPicBo;
import com.ruoyi.system.domain.vo.VariousKindsPicVo;
import com.ruoyi.system.domain.VariousKindsPic;
import com.ruoyi.system.mapper.VariousKindsPicMapper;
import com.ruoyi.system.service.IVariousKindsPicService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 照片Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-15
 */
@RequiredArgsConstructor
@Service
public class VariousKindsPicServiceImpl implements IVariousKindsPicService {

    private final VariousKindsPicMapper baseMapper;

    /**
     * 查询照片
     *
     * @param id 照片主键
     * @return 照片
     */
    @Override
    public VariousKindsPicVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询照片列表
     *
     * @param bo 照片
     * @return 照片
     */
    @Override
    public TableDataInfo<VariousKindsPicVo> queryPageList(VariousKindsPicBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VariousKindsPic> lqw = buildQueryWrapper(bo);
        Page<VariousKindsPicVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询照片列表
     *
     * @param bo 照片
     * @return 照片
     */
    @Override
    public List<VariousKindsPicVo> queryList(VariousKindsPicBo bo) {
        LambdaQueryWrapper<VariousKindsPic> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VariousKindsPic> buildQueryWrapper(VariousKindsPicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VariousKindsPic> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPicId()), VariousKindsPic::getPicId, bo.getPicId());
        lqw.eq(StringUtils.isNotBlank(bo.getPicSourceWebsite()), VariousKindsPic::getPicSourceWebsite, bo.getPicSourceWebsite());
        lqw.eq(StringUtils.isNotBlank(bo.getPicAddress()), VariousKindsPic::getPicAddress, bo.getPicAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getPicTitle()), VariousKindsPic::getPicTitle, bo.getPicTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getPicType()), VariousKindsPic::getPicType, bo.getPicType());
        return lqw;
    }

    /**
     * 新增照片
     *
     * @param bo 照片
     * @return 结果
     */
    @Override
    public Boolean insertByBo(VariousKindsPicBo bo) {
        VariousKindsPic add = BeanUtil.toBean(bo, VariousKindsPic.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改照片
     *
     * @param bo 照片
     * @return 结果
     */
    @Override
    public Boolean updateByBo(VariousKindsPicBo bo) {
        VariousKindsPic update = BeanUtil.toBean(bo, VariousKindsPic.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(VariousKindsPic entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除照片
     *
     * @param ids 需要删除的照片主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public VariousKindsPicVo selectRandomOne() {
        return BeanCopyUtils.copy(baseMapper.selectRandomOne(), VariousKindsPicVo.class);
    }
}
